package com.lizongbo.seotijian.rules;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import com.lizongbo.seotijian.util.HttpUtil;
import com.temesoft.google.pr.JenkinsHash;

/**
 * 
 * 
 * @see http://zh.wikipedia.org/zh/Favicon
 * @author lizongbo
 * 
 */
public class FaviconCheckRule extends AbstractCheckRule {

	@Override
	public boolean doCheck(URI uri) {
		String faviconUrl = uri.getScheme() + "://" + uri.getHost()
				+ (uri.getPort() == 80 ? "" : ":" + uri.getPort())
				+ "/favicon.ico";
		int code = 0;
		String contentType = "";
		try {
			URL url = new URL(faviconUrl);
			HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
			urlc.setConnectTimeout(1000);
			urlc.connect();
			code = urlc.getResponseCode();
			contentType = urlc.getContentType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(code + " " + contentType);
		return (code == 200 && contentType != null && contentType.toLowerCase()
				.startsWith("image/"));
	}

	public static void main(String[] args) throws URISyntaxException {
		String urlStr = "http://storyday.com";
		FaviconCheckRule rule = new FaviconCheckRule();
		rule.doCheck(new URI(urlStr));
	}
}
